<?php
/**
 * 
 * @authors Liujinbi (857053791@QQ.com)
 * @date    2017-07-29 17:00:05
 * @version $Id$
 */

namespace app\agent\controller;

use think\Db;
use think\Request;
use think\Config;
use think\Controller;

class Freqlist extends Shopbase {
    /**
     * 常用清单商品
     * @return [type] [description]
     */
    function recGoods(){
    	$data = Request()->param();
		$user_id = $this->shop_uid;
        $goods = Db::table('sdzx_goods')->alias('g')
        ->field('f.freq_id,g.goods_id,g.goods_name,g.unit,g.category_id_1,cate.cate_name,g.keywords,g.introduction,g.sort,g.picture,max(sval.price) as max_price,min(sval.price) as min_price,max(sval.is_hot) as is_hot,max(sval.is_recommend) as is_recommend,max(sval.is_new) as is_new,max(sval.is_cutprice) as is_cutprice,sum(sval.sales) as sales, max(sval.is_hot) as  is_promotion ')
        ->join('sdzx_goods_specval sval','g.goods_id=sval.goods_id and sval.status=1')
        ->join('sdzx_goods_category cate','g.category_id_1=cate.cate_id')
		->join('sdzx_user_freqlist f','g.goods_id=f.goods_id')
        ->where("sval.status=1 and f.user_id={$user_id}")
        ->group('g.goods_id')
        ->select();

        // 循环取出规格
        $tmp_list=[];
        foreach ($goods as $k => $v) {
            $tmp_sval = Db::connect([],'sval')->table('sdzx_goods')->alias('g')
            ->join('sdzx_goods_specval sval','g.goods_id=sval.goods_id')
            ->where("sval.goods_id={$v['goods_id']} and sval.status=1")->select();
            // $cur = array_shift($tmp_sval);
            $cur = $tmp_sval[0];
            // unset($cur['is_cutprice'],$cur['is_hot'],$cur['is_recommend'],$cur['is_new']);
            $tmp_goods = array_merge($goods[$k],$cur);
            if (sizeof($tmp_sval)>1) {
                $tmp_goods['list']=$tmp_sval;
            }
            $tmp_list[]=$tmp_goods;
        }

        // 商品归类
        $list=[];
        foreach ($tmp_list as $key => $value) {
            $list[$value['category_id_1']]['title']=$value['cate_name'];
            $list[$value['category_id_1']]['id']=$value['category_id_1'];
            $list[$value['category_id_1']]['list'][]=$value;
        }

	    //分类广告
	    foreach ($list as $key => $value) {
	    	$advlist_tim = Db::connect([],'adv')->field('adp.adp_name,adv.adv_name,adv.adv_id,adv.adv_url,adv.adv_img')
	    	->table('sdzx_goods_category')->alias('cate')
	    	->join('sdzx_adposition adp','cate.adp_id=adp.adp_id')
	    	->join('sdzx_adv adv','adp.adp_id=adv.adp_id')
	    	->where("cate.cate_id={$key}")
	    	->find();
            if ($advlist_tim) {
                $list[$key]['advlist']=$advlist_tim;
            }
	    	
	    }

	    $return_list=[];
	    foreach ($list as $key => $value) {
	    	array_push($return_list, $value);
	    }
	    return $this->fetch('',['list'=>$return_list]);

    }
	
	/* 删除 */
	public function del(){
		$freq_id=input('freq_id');
		$res=db('user_freqlist')->delete($freq_id);
		if($res){
			$this->success('操作成功');
		}else{
			$this->error('操作失败');
		}
	}
	
	/* 添加 */
	public function add_freq(){
		$uid = $this->shop_uid;
		// $specval_id = input('specval_id');
		$goods_id = input('goods_id');
		
		$info = db('user_freqlist')->where(['goods_id' => $goods_id,'user_id' => $uid])->find();
		if($info){
			$this->error('已经添加过','');
		}else{
			$data=[
				'user_id' => $uid,
				'goods_id' => $goods_id,
				'add_time' => date('Y-m-d H:i:s'),
				'shop_id' => $this->user['shop_id']
			];
			$freq_id = db('user_freqlist')->insertGetId($data);
			if($freq_id){
				$this->success('添加成功','');
			}else{
				$this->error('添加失败','');
			}
		}
	}
}